package com.itheima.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.DTO.OrderDTO;
import com.itheima.VO.OrderVO;
import com.itheima.common.entity.PageResult;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;


@Service
@Slf4j
public class OrderServiceImp implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    public PageResult pageQuery(OrderDTO orderDTO) {
        Integer currentPage = orderDTO.getCurrentPage();
        Integer pageSize = orderDTO.getPageSize();
        String queryString = orderDTO.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<OrderVO> page = orderMapper.pageQuery(queryString);

        return new PageResult(page.getTotal(), page.getResult());
    }

    public int update(Order OrderDTO){
        return orderMapper.updateOrderStatus(OrderDTO);
    }


    /**
     * 今日到诊数
     * @param reportDate
     * @return
     */
    public Integer todayVisitsNumber(LocalDate reportDate) {
        return orderMapper.todayVisitsNumber(reportDate);
    }

    //本周到诊数
    public Integer thisWeekVisitsNumber(LocalDate reportDate) {
        LocalDate monday = reportDate.with(DayOfWeek.MONDAY);
        reportDate = monday.plusDays(6);
        log.info("本周周一：{}",monday);
        log.info("本周周日：{}",reportDate);
        return orderMapper.thisWeekVisitsNumber(monday,reportDate);
    }

    //本月到诊数
    public Integer thisMonthVisitsNumber(LocalDate reportDate) {
        LocalDate firstDay = reportDate.withDayOfMonth(1);
        reportDate = firstDay.plusMonths(1).minusDays(1);
        log.info("本月第一天：{}",firstDay);
        log.info("本月最后一天：{}",reportDate);
        return orderMapper.thisMonthVisitsNumber(firstDay,reportDate);
    }
}
